﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using eCRAM.Model;
using eCRAM.Dao;
using eCRAM.Tools;

namespace eCRAM.WebSite.Models
{
    /// <summary>
    /// Modèle de recherche pour les métriques
    /// </summary>
    public class SearchMetricsModel
    {
        /// <summary>
        /// 
        /// </summary>
        public SearchActivityCriteriasModel Criterias { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public SearchActivityResultsModel Results { get; set; }

        public SearchMetricsModel()
        {
            Criterias = new SearchActivityCriteriasModel();
            Results = new SearchActivityResultsModel();
        }
    }


    public class SearchActivityCriteriasModel
    {
        /// <summary>
        /// Projet recherché
        /// </summary>
        [DisplayName("Projet")]
        public Guid ActivityId { get; set; }
    }


    public class SearchMetricCriteriasModel
    {

        /// <summary>
        /// Projet recherché
        /// </summary>
        [DisplayName("Projet")]
        public Guid ActivityId { get; set; }
    }



    public class CramByMonthModel
    {
        public int Month { get; set; }
        public int Year { get; set; }
        public IEnumerable<CramByActivityModel> CramsModel { get; set; }
        public IEnumerable<CramByUserModel> UsersModel { get; set; }


        public decimal Total
        {
            get { return UsersModel.Sum(u => u.Total); }
        }
        public int NbCrams { get { return CramsModel.Count(); } }
    }


    public class CramByActivityModel
    {
        public Guid ActivityId { get; set; }
        public Activity Activity { get; set; }

       
    }

    public class CramByUserModel
    {
        public User User { get; set; }
        public decimal Total { get; set; }
    }

    public class CramByStatusModel
    {
        public string Status { get; set; }
        public int Total { get; set; }
    }

    public class SearchActivityResultsModel
    {
        /// <summary>
        /// Identifiant
        /// </summary>

        public Guid Id { get; set; }

        /// <summary>
        /// Libelle de l'activité
        /// </summary>

        public string Name { get; set; }

        /// <summary>
        /// Date de début
        /// </summary>

        public DateTime StartDate { get; set; }

        /// <summary>
        /// Date de fin
        /// </summary>

        public DateTime? EndDate { get; set; }


        public Guid TypeId { get; set; }


        public bool IsActive { get; set; }


        public string TypeActivityLabel { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string ProjectLeader { get; set; }

        public IEnumerable<CramByMonthModel> CramsByActivity { get; set; }

        public IEnumerable<CramByUserModel> CramsByUser { get; set; }

        public IEnumerable<CramByStatusModel> CramsByStatus { get; set; }

        internal static SearchActivityResultsModel FromActivity(Activity activity)
        {
            if (activity == null)
                return null;
            return new SearchActivityResultsModel
                       {
                           Id = activity.Id,
                           EndDate = activity.EndDate,
                           IsActive = activity.IsActive,
                           Name = activity.Name,
                           StartDate = activity.StartDate,
                           TypeId = activity.Type,
                           TypeActivityLabel = activity.ActivityType.Label,
                           ProjectLeader = activity.User == null ? "" : activity.User.ToString(),
                           CramsByStatus = CramRepository.Search(null, null, null, null, new List< Guid>{ activity.Id})
                                .GroupBy(c => c.Status)
                                .OrderBy(c => (int)c.Key)
                                .Select( c => new CramByStatusModel {Status = c.Key.GetLabel(), Total = c.Count()}),
                           CramsByUser = CramRepository.GetCramsByActivityId(activity.Id)
                                .GroupBy(c => c.Cram.User)
                                .Select(c => new CramByUserModel { User = c.Key, Total = c.Where(a => a.ActivityId == activity.Id).Sum(cl => cl.Cram_DailyActivities.Sum(da => da.Value)) })
                                .OrderBy(c => c.User.LastName).ThenBy(c => c.User.FirstName),
                           CramsByActivity = CramRepository.Search(null, null, null, null, new List<Guid> { activity.Id })
                               .GroupBy(c => new { c.Month, c.Year, c, c.User })
                               .GroupBy(c => new { c.Key.Month, c.Key.Year },
                                        c => new CramByActivityModel { ActivityId = activity.Id })
                               .Select(c => new CramByMonthModel { Month = c.Key.Month, Year = c.Key.Year, CramsModel = c })
                               .OrderBy(c => c.Year).ThenBy(c => c.Month),
                       };
        }

    }
}